<?php
/*
<Controller Information> 
Author: Naseer Ahmad
Date: 23-10-2011
Last Updated on:
*/

class listingsController extends appController
{
	function index()
	{
		
		if(isset($this->data['cityID'])){
			$criteria="cityID=".$this->data['cityID']." AND status='Approved'";
			$this->set("cityID", $this->data['cityID']);
		}
		else if(isset($this->data['catID'])){
			$criteria="categoryID=".$this->data['catID']." AND status='Approved'";
			$this->set("catID", $this->data['catID']);
		}
		
		if(isset($this->data['sort'])) $sort=$this->data['sort']; else $sort="date";
		$this->set("sort", $sort);
		
		if($sort=="date")
			$order=" Order By isFeatured DESC, addedOn DESC";
		else
			$order=" Order By isFeatured DESC, price DESC";
		
		
		$ads = $this -> listings -> getAds($criteria.$order);
		$this -> set("ads", $ads);
		
		
		/////////// get sidebar listings
		if(isset($this->data['cityID'])){
			$regionID= $this -> listings -> _getSingleData("city", "regionID", "cityID=".$this->data['cityID']);
			$regionName = $this -> listings -> _getSingleData("regions", "regionName", "regionID=".$regionID);
			$this -> set("sidebarName", $regionName);
			$cities = $this -> listings -> _getArrayData("city", "*", "regionID=".$regionID);
			$this -> set("items", $cities);
			$this->set("sidebarLst","regions");
		}
		else{
			
			$mainCatID=$this -> listings -> _getSingleData("CategoriesInfo", "parentcategoryID", "categoryID=".$this->data['catID']);
			$catName = $this -> listings -> _getSingleData("CategoriesInfo", "categoryName", "categoryID=".$mainCatID);
			$this -> set("sidebarName", $catName);
			$cats = $this -> listings -> _getArrayData("CategoriesInfo", "*", "parentcategoryID=".$mainCatID);
			$this -> set("items", $cats);
			$this->set("sidebarLst","categories");
		}
		
		$this -> render("Listings");
	}
	
	
	function addToFavorites(){
/*		print "<pre>";
		print_r($this->data);
		print "</pre>";
*/		
		if(isset($_COOKIE['temp_member_id'])) 
			$tempUserID=$_COOKIE['temp_member_id'];
		else{
			$tempUserID= $this -> listings -> _getSingleData("favorites", "MAX(tempUserID)");
			if($tempUserID=="" || $tempUserID==0) $tempUserID=1; else $tempUserID++;
			setcookie("temp_member_id", $tempUserID, time()+60*60*24*30);
		}
		
		$fv=array("userID"	=> 0,
				  "adID"	=> $this->data['aid'],
				  "favoriteOn"	=> "NOW()",
				  "tempUserID"	=> $tempUserID
				);
		
		$this -> listings -> _insertTable("favorites", $fv);
		
		$rdArray=array("page"	=> "listings");
		if(isset($this->data['cityID'])) $rdArray['cityID']=$this->data['cityID'];
		if(isset($this->data['catID'])) $rdArray['catID']=$this->data['catID'];
		$rdArray['sort']=$this->data['sort'];
		$this -> redirect($rdArray);
	}
	
	
}
?>